# -*- coding: utf-8 -*-
# Create your views here.
from django.shortcuts import render_to_response, get_object_or_404, get_list_or_404
from django import forms
from django.http import HttpResponseRedirect, HttpResponse, Http404
from django.template import RequestContext
import os
from django.contrib.auth.decorators import login_required
from docs.models import *

@login_required
def project_list(request):
    project_objects = Project.objects.all()
    projects = []
    joined_projects = Project.objects.filter(translators = request.user)
    for project in project_objects:
        joined = project in joined_projects
        projects.append((project, joined))
    
    last_updated = Version.objects.filter(changed = True).latest('created').created
    people_count = User.objects.filter(joined_projects__isnull=False).distinct().count()
    translations_count = Translation.objects.filter(translator = request.user).count()
    effective_translations_count = Translation.effectives.filter(translator = request.user).count()
    title = "Project List"
    return render_to_response('docs/admin/project_list.html',locals(), context_instance = RequestContext(request))

@login_required
def project_join_quit(request, project_slug, action):
    project_object = get_object_or_404(Project, slug = project_slug)
    joined = request.user in project_object.translators.all()
    
    if (action=='join' and joined) or (action=='quit' and not joined):
        return HttpResponseRedirect(project_object.admin_url)
    
    if request.method == 'POST':
        if action=='join':
            project_object.translators.add(request.user)
        elif action=='quit':
            project_object.translators.remove(request.user)
        return HttpResponseRedirect(project_object.admin_url)
    
    title = "Confirmation of %s Project %s" % (action.title(), project_object)
    return render_to_response('docs/admin/confirmation.html',locals(), context_instance = RequestContext(request))

@login_required
def project_page_list(request, project_slug):
    project_object = get_object_or_404(Project, slug = project_slug)
    latest_version = project_object.versions.latest('created')
    content = {}
    effective_pages = project_object.effective_pages.select_related()
    for page in effective_pages:
        section = content.setdefault(page.section_slug,[])
        section.append(page)
#        
##    if project_object.content_order:
##        content_order = [''] + project_object.content_order.split()
##        content = sorted(content.items(), key = lambda(k,v):content_order.index(k))
##    else:
##        
    content = [[section, content[section]] for section in content]
    title = "%s | Documentation contents" % project_object
    return render_to_response('docs/admin/project_page_list.html',locals(), context_instance = RequestContext(request))


def project_versions(request, project_slug):
    return HttpResponse('project_versions')

def page(request, project_slug, page_slug):
    return HttpResponse('page')

def page_change(request, project_slug, page_slug):
    return HttpResponse('page_change')

@login_required
def people_list(request):
    people = User.objects.filter(pages__isnull = False).distinct()
    people_translations = []
    for u in people:
        ts = u.translations.all()
        effective_ts = ts.filter(inherited_translations__isnull = True)
        people_translations.append((u,ts.count(),effective_ts))
    people_translations.sort(key = lambda e: e[1], reverse = True)
    print people_translations
    title = "People List"
    return render_to_response('docs/admin/people_list.html',locals(), context_instance = RequestContext(request))

@login_required
def people(request, username):
    user_object = get_object_or_404(User, username = username)
    translations = []
    pages = user_object.pages.all().order_by('path')
    for page in pages:
        paragraphs = page.get_paragraphs().filter(translation__translator = user_object)
        translations.append((page, paragraphs))
    title = "Translations of People %s" % user_object
    return render_to_response('docs/admin/people.html',locals(), context_instance = RequestContext(request))

